home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
aminet
/
dev
/
lang
/
sbp3_1e.lzh
/
ISORT.PL
< prev
next >
Wrap
Text File
|
1991-10-31
|
810b
|
37 lines
/* From the book PROLOG PROGRAMMING IN DEPTH
by Michael A. Covington, Donald Nute, and Andre Vellino.
Copyright 1988 Scott, Foresman & Co.
Non-commercial distribution of this file is permitted. */
/* ISORT.PL */
/* Insertion sort */
/* To sort a list, sort its tail, then
insert its head in the right position. */
isort([Head|Tail],Result) :-
!,
isort(Tail,SortedTail),
insert(Head,SortedTail,Result).
isort([],[]).
/* To insert an item into the correct position
in a sorted list: Put it at the beginning
if it should precede the first element;
otherwise CDR down the list until a position
is found where this is the case. */
insert(X,[Y|Tail],[X,Y|Tail]) :-
X =< Y,
!.
insert(X,[Y|Tail],[Y|Z]) :-
X > Y,
!,
insert(X,Tail,Z).
insert(X,[],[X]).